package com.zyy.smartcity1129.home;

import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.google.android.material.tabs.TabLayout;
import com.youth.banner.Banner;
import com.youth.banner.indicator.CircleIndicator;
import com.zyy.smartcity1129.BaseFragment;
import com.zyy.smartcity1129.MainActivity;
import com.zyy.smartcity1129.R;
import com.zyy.smartcity1129.bus.BusFragment;
import com.zyy.smartcity1129.citysubway.CitySubwayFragment;
import com.zyy.smartcity1129.data.UserInfo;
import com.zyy.smartcity1129.data.all;
import com.zyy.smartcity1129.data.homeLbInfo;
import com.zyy.smartcity1129.data.newslist;
import com.zyy.smartcity1129.data.newstab;
import com.zyy.smartcity1129.hospital.HospitalFragment;
import com.zyy.smartcity1129.house.HouseFragment;
import com.zyy.smartcity1129.lifepay.LifePayFragment;
import com.zyy.smartcity1129.movie.MovieFragment;
import com.zyy.smartcity1129.news.News1Fragment;
import com.zyy.smartcity1129.news.NewsFragment;
import com.zyy.smartcity1129.parking.ParkingFragment;
import com.zyy.smartcity1129.takeout.TakeoutFragment;
import com.zyy.smartcity1129.util.BannerAd;
import com.zyy.smartcity1129.util.Http;
import com.zyy.smartcity1129.util.RvAd;
import com.zyy.smartcity1129.util.XCUtli;
import com.zyy.smartcity1129.work.WorkFragment;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class HomeFragment extends BaseFragment {
    private Button btn;
    private Banner banner;
    private RecyclerView rv1;
    private RecyclerView rv2;
    private TabLayout tab;
    private RecyclerView rv3;
    List<homeLbInfo.RowsBean> blist = new ArrayList<>();
    private List<all.RowsBean> alllist;
    private List<newstab.DataBean> tablist;
    private List<newslist.RowsBean> list;
    private UserInfo.UserBean userBean;
    public static UserInfo.UserBean userBeans;

    @Override
    protected int layId() {
        return R.layout.f_home;
    }

    @Override
    protected String title() {
        return "首页";
    }

    @Override
    protected boolean back() {
        return false;
    }

    @Override
    protected Boolean bottom() {
        return true;

    }

    @Override
    protected void init(View iv) {
        initView(iv);
        XCUtli xcUtli = new XCUtli(5);
        xcUtli.then(() -> {
            blist = Http.getInstance().get("/prod-api/api/rotation/list", null, homeLbInfo.class, 1).getRows();
            xcUtli.addint();
        });
        xcUtli.then(() -> {
            alllist = Http.getInstance().get("/prod-api/api/service/list", null, all.class, 1).getRows().subList(0, 9);
            xcUtli.addint();
        });
        xcUtli.then(() -> {
            tablist = Http.getInstance().get("/prod-api/press/category/list", null, newstab.class, 1).getData();
            xcUtli.addint();
        });
        xcUtli.then(() -> {
            list = Http.getInstance().get("/prod-api/press/press/list", null, newslist.class, 1).getRows();
            xcUtli.addint();
        });
        xcUtli.then(() -> {
            userBean = Http.getInstance().get("/prod-api/api/common/user/getInfo", null, UserInfo.class, 1).getUser();
            xcUtli.addint();
        });
        xcUtli.XCliveData.observe(getViewLifecycleOwner(), a -> {
            if (a.get() == 5) {
                userBeans = userBean;
                List<Object> b = new ArrayList<>();
                for (homeLbInfo.RowsBean all : blist) {
                    Log.i("TAG", all.getAdvImg());
                    b.add(all.getAdvImg());
                }
                banner.setAdapter(new BannerAd(b)).setBannerRound(16).addBannerLifecycleObserver(this).setLoopTime(3000).setIndicator(new CircleIndicator(getContext())).start();
                banner.setOnBannerListener(((o, i) -> {
                    NewsFragment.newsId = blist.get(i).getTargetId();
                    if (!(NewsFragment.newsId == 0)){
                        NewsFragment.names = list.stream().filter(a1->a1.getId() == blist.get(i).getTargetId()).findFirst().get().getTitle();
                        NewsFragment.typeId = list.stream().filter(a1->a1.getId() == blist.get(i).getTargetId()).findFirst().get().getType();
                        add(new News1Fragment());
                    }
                }));
                all.RowsBean a1 = new all.RowsBean();
                a1.setServiceName("全部服务");
                alllist.add(a1);
                RvAd<all.RowsBean> ad1 = new RvAd<all.RowsBean>(alllist, R.layout.r_all) {
                    private void initView(View vi) {
                        image = (ImageView) vi.findViewById(R.id.image);
                        tv1 = (TextView) vi.findViewById(R.id.tv1);
                    }

                    private TextView tv1;
                    private ImageView image;

                    @Override
                    protected void initR(View vi, all.RowsBean rowsBean, int p) {
                        initView(vi);
                        tv1.setText(rowsBean.getServiceName());
                        if (p == 9) {
                            Glide.with(image.getContext()).load(R.drawable.more).into(image);
                            vi.setOnClickListener(v->{
                                adds(tv1.getText().toString().trim());
                            });
                        } else {
                            Glide.with(image.getContext()).load(Http.base + rowsBean.getImgUrl()).into(image);
                            vi.setOnClickListener(v->{
                                adds(rowsBean.getServiceName());
                            });
                        }
                    }
                };
                rv1.setAdapter(ad1);
                rv1.setLayoutManager(new GridLayoutManager(getContext(), 5));
                ArrayList<newslist.RowsBean> list1 = new ArrayList<>();
                list1.addAll(list.subList(0, 4));
                RvAd<newslist.RowsBean> ad2 = new RvAd<newslist.RowsBean>(list1, R.layout.r_all) {
                    private void initView(View vi) {
                        image = (ImageView) vi.findViewById(R.id.image);
                        tv1 = (TextView) vi.findViewById(R.id.tv1);
                    }

                    private TextView tv1;
                    private ImageView image;

                    @Override
                    protected void initR(View vi, newslist.RowsBean rowsBean, int p) {
                        initView(vi);
                        tv1.setText(rowsBean.getTitle());
                        Glide.with(image.getContext()).load(Http.base + rowsBean.getCover()).into(image);
                    }
                };
                rv2.setAdapter(ad2);
                rv2.setLayoutManager(new GridLayoutManager(getContext(), 2));
                tab.removeAllTabs();
                for (newstab.DataBean dataBean : tablist) {
                    tab.addTab(tab.newTab().setText(dataBean.getName()));
                }
                List<newslist.RowsBean> list2 = new ArrayList<>();
                list2.addAll(list.stream().filter(a11 -> Integer.parseInt(a11.getType()) == tablist.get(0).getId()).collect(Collectors.toList()));
                RvAd<newslist.RowsBean> ad3 = new RvAd<newslist.RowsBean>(list2, R.layout.rv_base) {
                    private void initView(View iv) {
                        ig = (ImageView) iv.findViewById(R.id.ig);
                        tv1 = (TextView) iv.findViewById(R.id.tv1);
                        tv2 = (TextView) iv.findViewById(R.id.tv2);
                        tv3 = (TextView) iv.findViewById(R.id.tv3);
                        tv4 = (TextView) iv.findViewById(R.id.tv4);
                        tv5 = (TextView) iv.findViewById(R.id.tv5);
                    }

                    private TextView tv5;
                    private TextView tv4;
                    private TextView tv3;
                    private TextView tv2;
                    private TextView tv1;
                    private ImageView ig;

                    @Override
                    protected void initR(View vi, newslist.RowsBean rowsBean, int p) {
                        initView(vi);
//                        列表项包括图片、新闻标题、新闻内容缩写（多出的字用省略
//                        号显示）、评论总数、发布时间等信息。
                        Glide.with(ig.getContext()).load(Http.base + rowsBean.getCover()).into(ig);
                        tv1.setText(rowsBean.getTitle());
                        tv2.setText(rowsBean.getContent());
                        tv3.setText("评论总数："+rowsBean.getLikeNum());
                        tv4.setText("发布时间："+rowsBean.getUpdateTime());
                        tv5.setVisibility(View.GONE);
                    }
                };
                rv3.setAdapter(ad3);
                rv3.setLayoutManager(new LinearLayoutManager(getContext()));
                tab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                    @Override
                    public void onTabSelected(TabLayout.Tab tab) {
                        list2.clear();
                        list2.addAll(list.stream().filter(a11 ->Integer.parseInt(a11.getType()) == tablist.stream().filter(b->b.getName().equals(tab.getText())).findFirst().get().getId()).collect(Collectors.toList()));
                        ad3.notifyDataSetChanged();
                    }

                    @Override
                    public void onTabUnselected(TabLayout.Tab tab) {

                    }

                    @Override
                    public void onTabReselected(TabLayout.Tab tab) {

                    }
                });
            }
        });
    }

    public void adds(String name){
        switch (name){
            case "外卖订餐":
                add(new TakeoutFragment());
                break;
            case "生活缴费":
                add(new LifePayFragment());
                break;
            case "找工作":
                add(new WorkFragment());
                break;
            case "智慧巴士":
                add(new BusFragment());
                break;
            case "停哪儿":
                add(new ParkingFragment());
                break;
            case "城市地铁":
                add(new CitySubwayFragment());
                break;
            case "找房子":
                add(new HouseFragment());
                break;
            case "看电影":
                add(new MovieFragment());
                break;
            case "门诊预约":
                add(new HospitalFragment());
                break;
            case "活动管理":
                break;
            case "全部服务":
                MainActivity.bottom.setSelectedItemId(R.id.all);
                break;
        }
    }

    private void initView(View iv) {
        banner = (Banner) iv.findViewById(R.id.banner);
        rv1 = (RecyclerView) iv.findViewById(R.id.rv1);
        rv2 = (RecyclerView) iv.findViewById(R.id.rv2);
        tab = (TabLayout) iv.findViewById(R.id.tab);
        rv3 = (RecyclerView) iv.findViewById(R.id.rv3);
    }
}
